package com.scriverecanzoni.client;

import com.google.gwt.core.client.EntryPoint;
import com.google.gwt.core.client.GWT;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.HTML;
import com.google.gwt.user.client.ui.HorizontalPanel;
import com.google.gwt.user.client.ui.RootPanel;
import com.google.gwt.user.client.ui.TextBox;
import com.google.gwt.user.client.ui.VerticalPanel;

/**
 * Entry point classes define <code>onModuleLoad()</code>.
 */
public class AutorePerfettoEntryPoint implements EntryPoint {
	
	 // Create some fields for the UI
    VerticalPanel mainVerticalPanel = new VerticalPanel();
    HorizontalPanel hp1 = new HorizontalPanel();
    HorizontalPanel hp2 = new HorizontalPanel();
    HTML textLabel = new HTML("Text");
    HTML textwaccLabel = new HTML("Text With Accent");
    TextBox textTextBox = new TextBox();
    TextBox textwaccTextBox = new TextBox();
    Button submit = new Button("Submit");
	
 // Instantiate the interfaces to access methods in the interface
    private final PersistentServiceAsync persistentService = GWT
            .create(PersistentService.class);

	/**
	 * This is the entry point method.
	 */
	public void onModuleLoad() {
		
		hp1.add(textLabel);
	    hp1.add(textTextBox);
	    hp2.add(textwaccLabel);
	    hp2.add(textwaccTextBox);

	    mainVerticalPanel.add(hp1);
	    mainVerticalPanel.add(hp2);
	    mainVerticalPanel.add(submit);

	    hp1.setSpacing(5);
	    hp2.setSpacing(5);
	    mainVerticalPanel.setSpacing(5);
	        
        //Add a click handler to the button.  The click handler will
        //make an RPC call using the PersistentService interfaces
        submit.addClickHandler(new ClickHandler() {
            public void onClick(ClickEvent event) {

                persistentService.persistWord(textTextBox.getText(),
                		textwaccTextBox.getText(), new AsyncCallback<Void>() {

                            @Override
                            public void onFailure(Throwable caught) {

                                System.out
                                        .println("PersistentService RPC call failed "
                                                + caught);

                            }

                            @Override
                            public void onSuccess(Void result) {

                                System.out
                                        .println("PersistentService RPC call succedded");

                            }

                        });

            }
        });

        RootPanel.get("container").add(mainVerticalPanel);
    }
}